package com.lvmama.controller.nps;

import com.github.pagehelper.PageInfo;
import com.lvmama.client.nps.NPSClient;
import com.lvmama.common.enums.EnumForCategory;
import com.lvmama.common.enums.NpsQuestionTypeEnum;
import com.lvmama.common.enums.OrderTypeEnum;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.ExcelUtil;
import com.lvmama.pojo.dto.nps.NPSCommitDTO;
import com.lvmama.pojo.entity.nps.NPSCommit;
import com.lvmama.pojo.entity.nps.NPSStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/nps")
@Slf4j
public class NpsController {

    @Autowired
    NPSClient npsClient;


    @RequestMapping(value = "/getQuestionnaireList",method ={RequestMethod.GET,RequestMethod.POST})
    public String getQuestionnaireList(Model model) {
        try {
            // 品类
            model.addAttribute("categories", EnumForCategory.getAllCategory());
            model.addAttribute("orderTypes", OrderTypeEnum.values());
            model.addAttribute("npsQuestionTypes", NpsQuestionTypeEnum.values());
        } catch (Exception e) {
            log.error("getQuestionnaireList error:" + e.getMessage(), e);
        }
        return "nps/npsQuestionnaire";
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ResponseBody
    public Result<PageInfo<NPSCommit>> queryNpsCommitList(NPSCommitDTO npsCommitDTO){

        return npsClient.queryNpsCommitList(npsCommitDTO);
    }

    /**
     * 查询nps统计数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @RequestMapping(value = "/npsStatistics",method = RequestMethod.GET)
    @ResponseBody
    public Result<NPSStatistics> npsStatistics(NPSCommitDTO query) {
        log.info("get npsStatistics end.");
        return npsClient.npsStatistics(query);
    }

    /**
     * 导出问卷查询内容到excel
     *
     * @param query 查询条件
     */
    @RequestMapping(value = "/exportQuestionnaire",method = RequestMethod.GET)
    public void exportQuestionnaire(NPSCommitDTO query, HttpServletResponse response)  {
        String fileName;
        String[] title;
        List<Object[]> dataList = new ArrayList<>();
        try {
            if (NpsQuestionTypeEnum.INTERNAL.getCode().equals(query.getNpsQuestionType())) {
                fileName = "NPS问卷统计-内部";
                title = new String[]{"编号", "提交时间", "分值", "备注"};
            } else {
                fileName = "NPS问卷统计-外部";
                title = new String[]{"编号", "提交时间", "会员ID", "手机号", "品类", "订单ID", "提交时状态", "分值", "备注"};
            }
            Map<String,List<Object[]>>map= npsClient.exportQuestionnaire(query).getData();

            dataList = map.get("dataList");
            ExcelUtil excel = new ExcelUtil(fileName, title, dataList, response);
            excel.exportData();
        } catch (Exception e) {

        }



    }




}
